<template>
    <div class="loginBox">
      <el-form :model="formData" ref="formData" >
        <div class="formItem">
          <el-input
            placeholder="请输入手机号码"
            prefix-icon="el-icon-mobile-phone"
            :maxlength="11"
            @input="setNumberValue($event, 'phone')"
            autofocus
            v-model="formData.phone">
          </el-input>
        </div>
        <div class="formItem">
          <el-input
            placeholder="请输入短信验证码"
            prefix-icon="el-icon-message"
            @input="setNumberValue($event, 'code')"
            v-model="formData.validCode">
          </el-input>
          <div class="btn_get_code" style="margin-left: 5px" @click="getCode">{{countNum>0 ? countNum + 's' : '获取验证码'}}</div>
        </div>
      </el-form>
      <div class="errorTips">{{errorTips}}</div>
      <div class="btnGroup">
        <el-button class="btn" type="primary" style="width: 100%; padding: 17px; font-size: 16px" @click="submit">登 录</el-button>
      </div>
    </div>
</template>

<script>
  import api from '../../utils/api'
  export default {
    name: "phoneSms",
    data () {
      return {
        formData: {
          phone: '',
          validCode: '',
          randomId: ''
        },
        countNum: 0,
        countNumInterval: '',
        errorTips: ''
      }
    },
    methods: {
      getCode () {
        let phone = this.formData.phone
        if (!phone) {
          this.errorTips = '请填写有效手机号'
          return
        }
        if (!phone || !(/^1[345789]\d{9}$/.test(phone))) {
          this.errorTips = '手机号码录入有误，请重新填写'
          return
        }
        if (this.countNum > 0) return
        let params = {
          phone: this.formData.phone,
        }
        api.sendSms(params).then(res => {
          if (res.code === api.SUCCESS) {
            this.errorTips = ''
            this.formData.randomId = res.data
            this.countNum = 60
            this.countNumInterval = setInterval(() => {
              this.countNum--
              if (this.countNum < 1) {
                this.countNum = 0
                clearInterval(this.countNumInterval)
              }
            }, 1000)
          } else {
            this.errorTips = res.data
          }
        })
      },
      checkForm () {
        let {phone, validCode} = this.formData
        if (!phone || !(/^1[345789]\d{9}$/.test(phone))) {
          this.errorTips = '请填写有效手机号'
          return false
        }
        if (!validCode) {
          this.errorTips = '请填写短信验证码'
          return false
        }
        return true
      },
      submit () {
        if (!this.checkForm()) return
        this.$emit('submit', this.formData)
      },
      setNumberValue (event, key) {
        let val = event.replace(/[^0-9]/g, '');
        this.formData[key] = val
      }
    }
  }
</script>

<style>
  @import "login.css";
  .btn_get_code {
    white-space: nowrap;
    padding: 0 10px;
    display: flex;
    align-items: center;
    border: 1px solid #eee;
    border-radius: 3px;
    width: 100px;
    justify-content: center;
    cursor: pointer;
  }
  .errorTips {
    height: 30px;
    line-height: 30px;
    text-align: center;
    color: #f56c6c;
    margin-bottom: 20px;
  }
</style>
